package com.onstar.cn.vehicle.service;

import android.content.Context;

import com.onstar.cn.common.util.EncryptUtil;
import com.onstar.cn.sqlite.entities.PollingEntity;
import com.onstar.cn.sqlite.xutil.DbUtils;
import com.onstar.cn.sqlite.xutil.db.sqlite.Selector;
import com.onstar.cn.sqlite.xutil.db.sqlite.WhereBuilder;
import com.onstar.cn.sqlite.xutil.exception.DbException;
import com.onstar.cn.vehicle.entity.RequestStatus;

import java.util.ArrayList;
import java.util.List;
/**
 * 
* @ClassName: VehicleDBService 
* @Description: 轮训的数据库操作类
* @author 6335yz
* @date 2015年8月27日 下午8:03:22
 */
public class VehicleDBService {

	private Context context;
//	private PollingServiceImpl pollingServiceImpl;
	private DbUtils utils;

	public VehicleDBService(Context c,DbUtils utils) {
		this.context = c;
//		"onstar.db", 1
//		utils = DbUtils.create(c);
		this.utils = utils;
	}

	public void savePollingStatus(PollingEntity entity) {

		/*
		 * need to encrypt entity before save/update
		 */
		PollingEntity encryptEntity = entity.encrypt(context);
		try {
			utils.saveBindingId(encryptEntity);
		} catch (DbException e) {
			e.printStackTrace();
		}
//		return pollingServiceImpl.insert(encryptEntity);
	}

	public void updatePollingStatus(PollingEntity entity) {
//		pollingServiceImpl = new PollingServiceImpl(context);
		/*
		 * need to encrypt entity before save/update
		 */
		PollingEntity encryptEntity = entity.encrypt(context);
		try {
			utils.update(encryptEntity, WhereBuilder.b("requestId","=",
                    encryptEntity.getRequestId()).and("vin","=",encryptEntity.getVin()).and("requestType","=",encryptEntity.getRequestType()));
		} catch (DbException e) {
			e.printStackTrace();
		}
//		return pollingServiceImpl.update(encryptEntity, " requestId=? and vin=? and requestType=? ",
//				new String[] { encryptEntity.getRequestId(), encryptEntity.getVin(), encryptEntity.getRequestType() });
	}
	
	
	public void updatePollingStatusByVinAndType(PollingEntity entity) {
//		pollingServiceImpl = new PollingServiceImpl(context);
		/*
		 * need to encrypt entity before save/update
		 */
		PollingEntity encryptEntity = entity.encrypt(context);
		try {
			PollingEntity mEntity = utils.findFirst(Selector.from(PollingEntity.class).where("vin","=",encryptEntity.getVin())
					.and("requestType","=",encryptEntity.getRequestType()));
			if(mEntity != null){
				utils.update(encryptEntity,WhereBuilder.b("vin","=",encryptEntity.getVin())
						.and("requestType","=",encryptEntity.getRequestType()));
			}else{
				utils.saveBindingId(encryptEntity);
			}

		} catch (DbException e) {
			e.printStackTrace();
		}
//		return pollingServiceImpl.update(encryptEntity, "   vin=? and requestType=? ",
//				new String[] { encryptEntity.getVin(), encryptEntity.getRequestType() });
	}

	public List<PollingEntity> getInprogressPollingStatus(String vin) {
//		pollingServiceImpl = new PollingServiceImpl(context);
		/*
		 * need to encrypt entity before search requestStatus should is 'in
		 * progress'
		 */
		vin = EncryptUtil.encrypt(context, vin);
		String orderBy = " startTime ";
//		List<PollingEntity> entities = pollingServiceImpl.getAll(null, "vin=? and requestStatus=? ",
//				new String[] { vin, EncryptUtil.encrypt(context, RequestStatus.InProgress.getStringValue()) }, null, null,
//				orderBy, null);
		List<PollingEntity> entities = null;
		try {
			entities = utils.findAll(Selector.from(PollingEntity.class).where("vin","=",vin)
                    .and("requestStatus","=", EncryptUtil.encrypt(context, RequestStatus.InProgress.getStringValue())).orderBy(orderBy));
		} catch (DbException e) {
			e.printStackTrace();
		}
		List<PollingEntity> decryptList = new ArrayList<PollingEntity>();
		if(entities != null) {
			for (PollingEntity pollingEntity : entities) {
				decryptList.add(pollingEntity.decrypt(context));
			}
		}

		return decryptList;
	}

	/*
	 * default current vin
	 */
	public List<PollingEntity> getInprogressPollingStatusByType(String vin, String requestType) {
//		pollingServiceImpl = new PollingServiceImpl(context);
		requestType = EncryptUtil.encrypt(context, requestType);
		vin = EncryptUtil.encrypt(context, vin);
//		List<PollingEntity> entities = pollingServiceImpl.getAll(null, "vin=? and requestStatus=? and requestType=? ",
//				new String[] { vin, EncryptUtil.encrypt(context, RequestStatus.InProgress.getStringValue()), requestType },
//				null, null, null, null);
		List<PollingEntity> entities = null;
		try {
			entities = utils.findAll(Selector.from(PollingEntity.class).where("vin","=",vin)
                    .and("requestStatus","=", EncryptUtil.encrypt(context, RequestStatus.InProgress.getStringValue())).and("requestType","=",requestType));
		} catch (DbException e) {
			e.printStackTrace();
		}
		List<PollingEntity> decryptList = new ArrayList<PollingEntity>();
		for (PollingEntity pollingEntity : entities) {
			decryptList.add(pollingEntity.decrypt(context));
		}
		return decryptList;
	}

	/*
	 * default current vin
	 */
	public void deletePollingStatusByType(String requestType, String vin) {
		requestType = EncryptUtil.encrypt(context, requestType);
		vin = EncryptUtil.encrypt(context, vin);
//		pollingServiceImpl = new PollingServiceImpl(context);
		try {
			utils.delete(PollingEntity.class,WhereBuilder.b("vin","=",vin).and("requestType","=",requestType));
		} catch (DbException e) {
			e.printStackTrace();
		}
//		long rowId = pollingServiceImpl.delete("vin=? and requestType=? ", new String[] { vin, requestType });
//		return rowId;
	}

	public void deletePollingStatusByVin(String vin) {
		vin = EncryptUtil.encrypt(context, vin);
//		pollingServiceImpl = new PollingServiceImpl(context);
//		long rowId = pollingServiceImpl.delete("vin=? ", new String[] { vin });
		try {
			utils.delete(PollingEntity.class,WhereBuilder.b("vin","=",vin));
		} catch (DbException e) {
			e.printStackTrace();
		}

	}
	
	public void updatePollingStatusToFail(String vin) {
//		pollingServiceImpl = new PollingServiceImpl(context);
		List<PollingEntity> entities = getInprogressPollingStatus(vin);
		for (PollingEntity pollingEntity : entities) {
			pollingEntity.setRequestStatus(RequestStatus.Failure.getStringValue());
			updatePollingStatus(pollingEntity);
		}
	}
}
